package com.cy.pj.sys.service.impl;


import com.cy.pj.common.web.pojo.Input;
import com.cy.pj.common.web.pojo.JSONObject;
import com.cy.pj.sys.dao.ApprovalDao;
import com.cy.pj.sys.dao.ProcessDao;
import com.cy.pj.sys.pojo.Approval;
import com.cy.pj.sys.pojo.Process;
import com.cy.pj.sys.service.IApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApprovalService implements IApprovalService {
    @Autowired
    ProcessDao processDao;
    @Autowired
    ApprovalDao approvalDao;

    @Override
    public void addApproval(Input input) {
        input.getApproval().setApplicant(input.getUser());
        Process process = processDao.selectProcess(input.getUser().getDeptId(), 1);
        input.getApproval().setProcess(process);
        approvalDao.insertApproval(input.getApproval());
    }

    @Override
    public JSONObject findApprovals(Approval approval) {
        return new JSONObject(1,"查询成功",approvalDao.selectApprovals(approval));
    }

    @Override
    public void passApproval(Input input) {
        Approval approval = input.getApproval();
        approvalDao.updateApproval(approval);

        Process process = processDao.selectById(approval.getProcessId());
        if (process.getNextId() != null) {
            process = processDao.selectNextProcess(process.getNextId());
            approval.setProcess(process);
        } else {
            approval.setProcessId(null);
        }
    }

    @Override
    public void rejectApproval(Input input) {
        Approval approval = input.getApproval();
        approvalDao.updateApproval(approval);
    }
}












